<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use SixShop\Wangdian\WangdianFactory;
use SixShop\Wangdian\Exception\ApiException;

// Create client
$client = WangdianFactory::createSandboxClient(
    sid: 'your-sid',
    appKey: 'your-app-key',
    appSecret: 'your-app-secret'
);

try {
    // Push a trade order
    echo "Pushing trade order...\n";
    
    $tradeData = [
        'shop_no' => 'api_test',
        'switch' => 0,
        'trade_list' => [
            [
                'tid' => 'SDK_TEST_' . time(),
                'trade_status' => 30,
                'delivery_term' => 1,
                'pay_status' => 2,
                'trade_time' => date('Y-m-d H:i:s'),
                'pay_time' => date('Y-m-d H:i:s'),
                'buyer_nick' => 'test_buyer',
                'buyer_email' => 'test@example.com',
                'receiver_mobile' => '13800138000',
                'receiver_telno' => '',
                'receiver_zip' => '100000',
                'receiver_province' => '北京',
                'receiver_name' => '张三',
                'receiver_city' => '北京市',
                'receiver_district' => '海淀区',
                'receiver_address' => '测试地址123号',
                'logistics_type' => 4, // EMS
                'invoice_type' => 0,
                'invoice_title' => '',
                'invoice_content' => '',
                'buyer_message' => 'SDK测试订单',
                'remark' => 'SDK测试专用',
                'remark_flag' => 1,
                'post_amount' => 10.0,
                'paid' => 109.0,
                'cod_amount' => 0.0,
                'ext_cod_fee' => 0.0,
                'order_list' => [
                    [
                        'oid' => 'SDK_ITEM_' . time(),
                        'status' => 30,
                        'refund_status' => 0,
                        'goods_id' => 'TEST_GOODS_001',
                        'spec_id' => '',
                        'goods_no' => 'TEST001',
                        'spec_no' => 'TEST001-001',
                        'goods_name' => '测试商品',
                        'spec_name' => '测试规格',
                        'num' => 1,
                        'price' => 99.0,
                        'adjust_amount' => 0.0,
                        'discount' => 0.0,
                        'share_discount' => 0.0,
                        'cid' => '1',
                    ]
                ]
            ]
        ]
    ];
    
    $response = $client->trade()->push($tradeData);
    
    if ($response->isSuccess()) {
        echo "Trade pushed successfully!\n";
        echo "Response: " . $response->toJson() . "\n";
    } else {
        echo "Trade push failed: " . $response->getMessage() . "\n";
    }
    
    // Query recent trades
    echo "\nQuerying recent trades...\n";
    
    $queryParams = [
        'start_time' => date('Y-m-d 00:00:00'),
        'end_time' => date('Y-m-d 23:59:59'),
    ];
    
    $response = $client->trade()->query($queryParams);
    
    if ($response->isSuccess()) {
        $trades = $response->getData();
        echo "Found " . count($trades) . " trades today\n";
        
        foreach (array_slice($trades, 0, 5) as $trade) {
            echo "- TID: {$trade['tid']}, Status: {$trade['trade_status']}, Amount: {$trade['paid']}\n";
        }
    } else {
        echo "Trade query failed: " . $response->getMessage() . "\n";
    }

} catch (ApiException $e) {
    echo "API Error: " . $e->getMessage() . "\n";
    echo "API Code: " . $e->getApiCode() . "\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}